﻿//Søren Pedersen ANNO 13-9-2011
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary;
using Model;
using Interfaces;


namespace Networking
{
    class HandleServer
    {
        TcpClient client;
        Thread clientthread;
        ServerController servercontroller;
        AuktionCollection auktioncollection;

        public HandleServer(ServerController servercontroller, AuktionCollection auktioncollection)
        {
            this.servercontroller = servercontroller;
            this.auktioncollection = auktioncollection;
            client = new TcpClient();
            try
            {
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001));
            }
            catch (SocketException e)
            {
                servercontroller.SocketException(e.ToString());
            }
        }

        public void Listen()
        {
            if (client.Connected)
            {
                NetworkStream stream = client.GetStream();
                StreamReader reader = new StreamReader(stream);
                
                //så længe at CanRead er true da den bliver false hvis der sker en fejl
                while (true)
                {
                        //string message = reader.ReadLine();
                        IFormatter formatter = new BinaryFormatter();
                        //object message = formatter.Deserialize(stream);
                        if (stream.DataAvailable)
                        {
                            try
                            {
                                List<IAuktion> auktioner = (List<IAuktion>)formatter.Deserialize(stream);
                                DecodeMessage(auktioner);
                            }
                            catch (SerializationException e)
                            {
                                String besked = reader.ReadLine();
                                DecodeMessage(besked);

                            }
                        }
                }
                reader.Dispose();
            }
        }

        public void DecodeMessage(String message)
        {
            
        }

        //public void DecodeMessage(object message)
        //{
        //    auktioncollection.TilføjAuktion((Auktion)message);
        //    List<IAuktion> auktioner = new List<IAuktion>();
        //    auktioner = (List<IAuktion>)message;
        //    foreach (IAuktion auktion in auktioner)
        //    {
        //        Console.WriteLine(auktion.Navn);
        //    }
        //}

        public void DecodeMessage(List<IAuktion> auktioner)
        {
            auktioncollection.TilføjAuktioner(auktioner);
        }

        public void Send(string message)
        {
            NetworkStream stream = client.GetStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine(message);
            writer.Flush();
        }
    }
}
